<?php

namespace app\manage\controller\admin\sys;

use buwang\base\AdminBaseController;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="城市表")
 */
class City extends AdminBaseController
{
    /**
     * 允许修改的字段
     * @var array
     */
    protected $allowModifyFileds = [
        'status',
        'sort',
        'remark',
        'is_delete',
        'is_auth',
        'title',
        'is_show',
    ];

    use \buwang\traits\Crud;

    public function initialize()
    {
        parent::initialize();
        $this->layout && $this->app->view->engine()->layout($this->layout);
        $this->model = new \app\common\model\system\SysCity();
        $this->assign('getIsShowList', $this->model->getIsShowList());

    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            //查询数据表是否存在
            $table_name = '_sys_city';
            $table_temp = Db::query("SHOW TABLE STATUS LIKE '%{$table_name}%'");
            if(empty($table_temp)) {
                return $this->error('数据表'.env('database.prefix', 'bw_').$table_name.'不存在，请先导入表数据');
            }

            if (input('selectFieds')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'total' => $count,
                'list' => $list,
            ];
            return $this->success('success', $data);
        }
        return view();
    }

    /**下载基础sql
     * @return \think\response\File
     */
    public function download()
    {
        $template_image = root_path().DS.'app'.DS.'manage'.DS.'source'.DS.'city'.DS.'datebase.sql';
        return download($template_image, '城市数据.sql');
    }

    
}